package com.edu.chat.web.common.utils;


import cn.hutool.core.lang.UUID;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-04-06 20:21
 **/

@Slf4j
@Component
public class QrUtil {

	@Autowired
	private MinioUtil minioUtil;

	private static final String BUCKET_NAME = "qrcode";

	private static final String LOGO_PATH = "static/logo.png";
	private static final QrConfig QR_CONFIG = QrConfig.create()
			.setImg(LOGO_PATH)
			.setForeColor(new Color(73, 151, 196))  // 明确蓝色RGB值
			.setBackColor(new Color(255, 255, 255))  // 明确粉色RGB值
			.setRatio(8)
			.setErrorCorrection(ErrorCorrectionLevel.M)
			.setMargin(1);

	public String getQrCode(String url) {
		try {
			byte[] bytes = QrCodeUtil.generatePng(url, QR_CONFIG);

			String fileName = BUCKET_NAME + System.currentTimeMillis()
					+ "_" + UUID.randomUUID().toString().substring(0, 8) + ".png";
			minioUtil.upload(bytes, fileName);

			return minioUtil.getFileUrl(fileName);
		} catch (Exception e) {
			// 添加日志记录：log.error("QR code generation failed", e);
			throw new RuntimeException("Failed to generate QR code", e);
		}
	}


}
